import { AnyAction, Reducer } from 'redux';
import { EffectsCommandMap } from 'dva';
import { message } from 'antd';
import {
  queryPrivateCustomersA,
  createPrivateCustomer,
  baseInfo,
  deletePrivateCustomer,
  getPrivateCustomer,
  queryPrivateCustomers,
  queryAllPrivateCustomers,
} from './service';
import { TableListData } from './data.d';
import Constants from '../declareManagement/utils/Constants';
const R = require('ramda');
const productAddTempIdPrefix = 'productAddTempIdPrefix_';
export interface StateType {
  data: TableListData;
}

export type Effect = (
  action: AnyAction,
  effects: EffectsCommandMap & { select: <T>(func: (state: StateType) => T) => T },
) => void;

export interface ModelType {
  namespace: string;
  state:{
    page: {},
    list: [],
    dataSource: {},
    type: [],
    status: [],
    supplier: [],
    buyerList:{},
    StateType
  },
  effects: {
    fetch: Effect;
    add: Effect;
    remove: Effect;
    update: Effect;
  };
  reducers: {
    save: Reducer<StateType>;
  };
}

const Model: ModelType = {
  namespace: 'declareManagement',

  state: {
    list: [],
    buyerList: [], //境外买家
    buyer:{},
  },

  effects: {
    *fetch({ payload }, { call, put }) {
      payload.type = 1;
      const response = yield call(queryPrivateCustomers, payload);
      if (response.resultCode === 'ok') {
        if (!R.isEmpty(response.content) && !R.isNil(response.content)) {
          const { page, list } = response.content;
          yield put({
            type: 'refreshState',
            payload: {
              list,
              pagination: { current: page.page, total: page.totalRow, pageSize: page.size },
            },
          });
        } else {
          let page = Constants.DEFAULT_PAGE;
          yield put({
            type: 'refreshState',
            payload: {
              list: [],
              pagination: { current: page.page, total: page.totalRow, pageSize: page.size },
            },
          });
        }
      }
    },

    /**
     * 新增报关抬头
     * @param {any} payload
     * @param {any} call
     */
      *createPrivateCustomer({payload},{  call, put, select }){
      const dataSource = payload.dataSource;

      const response = yield call(createPrivateCustomer, {...R.omit(['onSuccess', 'onFail'])(dataSource) });

      if(response.resultCode === "ok"){
        message.info("新增报关抬头成功！");
        if(payload.onSuccess) payload.onSuccess();
      } else {
        message.error(response.errMessage)
        if(payload.onFail) payload.onFail();
      }
    },

    /**
     * 更新报关抬头
     * @param payload
     * @param call
     * @param put
     * @param select
     */
      *baseInfo({ payload },{ call, put }){
      const dataSource = payload.dataSource;
      const response = yield call(baseInfo, { ...R.omit(['onSuccess', 'onFail'])(dataSource) });
      if(response.resultCode === "ok"){
        message.info("更新报关抬头成功！");
        if(payload.onSuccess) payload.onSuccess();
      } else {
        if(payload.onFail) payload.onFail();
      }
    },

    /**
     * 删除报关抬头
     * @param {any} payload
     * @param {any} call
     */
    *deletePrivateCustomer({ payload },{ call }){
      const response = yield call(deletePrivateCustomer,{ ...R.omit(['onSuccess', 'onFail'])(payload) });
      if(response.resultCode === "ok"){
        message.info("删除报关抬头成功！");
        if (payload.onSuccess) payload.onSuccess();
      } else {
        if(payload.onFail) payload.onFail();
      }
    },

    /**
     * 根据ID查询报关抬头详情
     * @param {any} payload
     * @param {any} call
     * @param {any} put
     * @param {any} select
     */
    *getPrivateCustomer({ payload }, { call, put, select }) {
      const response = yield call(getPrivateCustomer, { id: payload.id });
      if (response.resultCode === 'ok') {
        yield put({
          type: 'refreshState',
          payload: {
            buyerList: response.content,
          }
        });
      } else {
        message.error(response.errMessage);
      }
    },

    /**
     * 根据id删除报关抬头
     *
     * @param id
     * @returns {Promise.<void>}
     */
    *deleteOrderById({ payload, callback }, { call, put }) {
      const response = yield call(deleteOrderById, { id: payload.id });
      if (response.resultCode === 'ok') {
        message.info('删除报关资料成功!');
        if (callback) callback();
      }
    },

    /**
     * 查询个人客户
     *
     * @param call
     * @param put
     */
    *queryPrivateCustomers({ payload }, { call, put }) {
      const response = yield call(queryPrivateCustomers, { ...payload });
      if (response.resultCode === 'ok') {
        yield put({
          type: 'refreshState',
          payload: {
            list1: response.content.list,
            page: response.content.page
          },
        });
      } else {
        yield put({ type: 'refreshState', payload: { privateCstList: [] } });
      }
    },


    /**
     * 查询报关抬头
     * @param {any} payload
     * @param {any} call
     * @param {any} put
     */
    *queryAllPrivateCustomers({ payload },{ call, put }){
      const response = yield call(queryAllPrivateCustomers, { ...R.omit(['onSuccess', 'onFail'])(payload) });
      if(response.resultCode === "ok"){
        yield put({
          type: 'refreshState',
          payload:{
            List2: R.isNil(response.content.list) ? [] : response.content.list
          }
        });
        yield put({
          type: 'refreshState',
          payload: {
            cstId:response.content.cstId,
            cstName:response.content.cstName,
          }
        });
      } else {
        if(payload.onFail) payload.onFail();
      }
    },




  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        data: action.payload,
      };
    },
    /**
     * 设置业务参数
     *
     * @param state
     * @param payload
     */
    setBizParams(state, { payload }) {
      let newState = state.bizParams;
      if (payload.sprCode != undefined) {
        newState[payload.sprCode] = payload.list;
      }
      return { ...state, bizParams: newState };
    },

    /**
     * 刷新state
     *
     * @param state
     */
    refreshState(state, { payload }) {
      return { ...state, ...payload };
    },
  },
};

export default Model;
